package com.itts.hk.utils;


import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class leetcode00 {
    @Test
    public void main() {
        String a = "abcfgacbdbbfg";
        int i = test1(a);
        System.out.println(i);
        int i1 = test2(a);
        System.out.println(i1);

    }

    private int test1(String s) {
        // 记录字符上一次出现的位置
        int[] last = new int[128];
        for (int i = 0; i < 128; i++) {
            last[i] = -1;
        }
        int n = s.length();
        int res = 0;
        int start = 0; // 窗口开始位置
        for (int i = 0; i < n; i++) {
            //String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引，序列的第一个char值是在索引为0，索引1，依此类推
            int index = s.charAt(i);
            start = Math.max(start, last[index] + 1);  // a >= b ? a : b
            res = Math.max(res, i - start + 1);
            last[index] = i;
        }

        return res;
    }

    private int test2(String s) {
        if (s.length() == 0)
            return 0;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        int max = 0;
        int left = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                left = Math.max(left, map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i), i);
            max = Math.max(max, i - left + 1);
        }
        return max;
    }

    @Test
    public void test3(){
        String a = "abaf";
        int b = a.charAt(2);
        char  h = a.charAt(2);
        System.out.println(b);
        System.out.println(h);

    }

}
